import ViewBase from "../../engine/ViewBase";
import { SocketClient } from "../../net/SocketClient";
import { UserModel } from "../model/UserModel";
import { UIHelper } from "../util/UIHelper";


const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewExchangeCoin extends ViewBase {

    private txtExchangeNum: cc.EditBox

    onEnable(): void {
        super.onEnable()
        this.txtExchangeNum = this.getChild(this.node, "txtExchangeNum", cc.EditBox)
        let btnExchange: cc.Node = this.findChild(this.node, "btnExchange")
        UIHelper.onAddEvent(btnExchange, () => {
            if (this.txtExchangeNum.string == "") {
                UIHelper.showTip("请输入兑换数量")
                return;
            }
            let num: number = parseInt(this.txtExchangeNum.string)
            this.onClickExchange(num)
        })
        let btnExchangeAll: cc.Node = this.findChild(this.node, "btnExchangeAll")
        UIHelper.onAddEvent(btnExchangeAll, () => {
            this.onClickExchange(UserModel.instance.income)
        })
        this.refresh()
    }

    private onClickExchange(num: number) {
        SocketClient.req_exchange_coin(num, () => {
            this.refresh()
        })
    }

    private refresh() {
        let txtMax: cc.Label = this.getChild(this.node, "txtMax", cc.Label)
        txtMax.string = "最高可兑换" + UserModel.instance.income

    }




}
